
import 'package:fijkplayer/fijkplayer.dart';
import 'package:flutter/material.dart';
import 'package:videopiclist/utils.dart';

import 'beans/VideoItem.dart';

void main() {
  runApp(
    MaterialApp(
      home: MyApp(),
    ),
  );
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  FijkPlayer player = FijkPlayer();
  List<VideoItem> list = [];
  @override
  void initState() {
    super.initState();
    list = Utils.getData();
  }

  @override
  void dispose() {
    super.dispose();
    player.release();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("视频与图片混合列表"),
      ),
      body: Container(
          child: ListView.builder(
              itemCount: list.length,
              itemBuilder: (BuildContext context, int position) {
                // List items = dataNet["data"];
                var item = list[position];
                return Container(
                  height: MediaQuery.of(context).size.height / 2,
                  child: Column(
                    children: [
                      Text("$position-" + item.title),
                      getVideoOrPic(list, player, position),
                    ],
                  ),
                );
              })),
    );
  }


  getVideoOrPic(List<VideoItem> list, FijkPlayer player, int position) {
    var item = list[position];
    print("item--$item");
    if (item.type == "video") {
      if (item.isPlay) {
        // player.stop();
        // player.pause();
        player.reset();
        player.setDataSource(item.url, autoPlay: true);
        return Container(
          child: FijkView(
            player: player,
            height: MediaQuery.of(context).size.height / 2 - 50,
          ),
        );
      } else {
        return GestureDetector(
          child: Image.network(item.icon),
          onTap: () {
            setState(() {
              for (int i = 0; i < list.length; i++) {
                if (i == position) {
                  list[i].isPlay = true;
                } else {
                  list[i].isPlay = false;
                }
              }
            });
          },
        );
      }
    } else {
      return Image.network(
        item.url,
        height: MediaQuery.of(context).size.height / 2 - 50,
      );
    }
  }
}
